using System;
using System.IO;
using System.Text;

namespace ThalesHsmComms
{
	public class GenerateKeyCheckValueMsg : WSMMsg
	{
		private String encryptedKey;
		public String CheckDigits{private set;get;}
		private String keyType;

		public GenerateKeyCheckValueMsg (String encrypted_key, String keyType)
			:base("BU")
		{
			this.encryptedKey = encrypted_key;
			this.keyType = keyType;
		}

		override protected void Encode()
		{
			
			StringBuilder data = new StringBuilder();
			
			try 
			{
				// pack command code
				data.Append(this.commandCode);
				
				// pack lmk pair
				data.Append("FF");
				
				// pack key length flag
				data.Append("1");
				
				// pack key
				data.Append("U");
				data.Append(this.encryptedKey);
				
				// pack delimiter
				data.Append(";");
				
				// pack keytype
				data.Append(this.keyType);
				
				// pack delimiter
				data.Append(";");
				
				// pack keyscheme zmk
				data.Append("U");
				
				// pack keyscheme lmk
				data.Append("U");
				
				// pack kcv
				data.Append("0");
				
				this.requestData = Encoding.ASCII.GetBytes(data.ToString());
				
			} 
			catch (IOException ex) 
			{
				Console.WriteLine(ex);
			}
		}

		override protected void Decode()
		{
			// get key check value
			this.CheckDigits = this.GetNextBytesFromResponse(16);
			
		}
	}
}

